# -*- coding: utf-8 -*-
from PhysicObject import *
from settings import *
import random

DISPLAY_SIZE = (800, 600)
JOKER = 2

class Block(PhysicObject):
    #image = load_image('images/block_speed_right.png').convert_alpha()

    images = load_image(['images/block_wood.png','images/block_speed_right.png',
                    'images/block_gray.png','images/block_pink.png','images/block_purple.png',
                    'images/block_yellow.png','images/block_green.png','images/block_blue.png',
                    'images/block_red.png','images/block_metal.png','images/block_joker.png'])
    
    def __init__(self, world, x, y, dinamyc=1, restitucion=0.0, metal=0):
        self.world = world
        self.bodyDef = box2d.b2BodyDef()
        self.bodyDef.position = (x, y)
        self.bodyDef.userData = self
        self.bodyDef.isBullet = True
        self.bodyDef.allowSleep = True
        if metal:
            self.bodyDef.allowSleep = False
        self.body = self.world.CreateBody(self.bodyDef)
        self.shapeDef = box2d.b2PolygonDef()
        self.shapeDef.SetAsBox(2.2, 2.2)
        self.shapeDef.density = 100
        self.shapeDef.restitution = restitucion
        self.shapeDef.friction = 0.05
        if metal:
            self.shapeDef.friction = 10.0  
        if metal is JOKER and not dinamyc:
            self.shapeDef.friction = 0.05 
            self.shapeDef.restitution = 5.0
        self.shape=self.body.CreateShape(self.shapeDef)

        print metal

        if not dinamyc:
            index=0
            print 'not dinamyc'
            if metal:
                index=9
            if metal is JOKER:
                print 'joker'
                index = 10           
        else:
            self.body.SetMassFromShapes()
            index=random.randint(1, 8)

            #print index
        
        self.image = self.images[index]

    def draw( self, surface, offset_x=0, offset_y=0 ):
        offset_x = 25 
        offset_y = 25
        (screen_width,screen_height) = DISPLAY_SIZE
        pos = self.body.GetPosition()

        rotated = pygame.transform.rotate(self.image, -self.body.angle*180/3.14)
        
        rotation_offset_x = (rotated.get_width() - self.image.get_width())/2
        rotation_offset_y = (rotated.get_height() - self.image.get_height())/2


        surface.blit(rotated, (int(pos.x*TILE_SIZE)-offset_x-rotation_offset_x,
                     int(pos.y*TILE_SIZE)-offset_y-rotation_offset_y))

#        print 	self.body.IsFrozen()
#        print 	self.body.IsSleeping()


    def sleeping( self):
        return self.body.IsSleeping()

    def isAllowSleep(self):
        """Indica si tiene permitido dormirse el objeto"""
        return self.bodyDef.allowSleep

    def destroy( self):
        """Destruye el objeto """
        self.body.DestroyShape (self.shape)
